package com.yuanqi.dz.dc.adapters;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqi.dz.dc.info.DZFieldConfig;
import com.yuanqi.dz.dc.info.MainConfig;
import com.yuanqi.dz.dc.info.MerchantConfig;
import com.yuanqi.dz.dc.util.DateUtil;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class DCReadFile {
	public static String accountDate=null;
	
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");
	
	public DCReadFile(String dz_date){
		accountDate=dz_date;
	}

	public  DZInfoArray  readFile(File  file,String merchant_num,String dzdate,MerchantConfig merchantConfig){
		DZInfoArray dzInfoArray = new DZInfoArray(); 
		MainConfig mainConfig=merchantConfig.getMainConfig();
		Map<String, Map<String,DZFieldConfig>> map=merchantConfig.getMap();
		Map<String,DZFieldConfig> S1MAP=map.get("S1");
		String is_fiter="1";
		String charset=mainConfig.getCharset();
		if(file.exists()){
			BufferedReader reader=null;
			List<DZInfo> list=new ArrayList<DZInfo>();
			try {
				reader =StringHelper.isEmpty(charset) ? new BufferedReader(new FileReader(file)): new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName(charset)));
				String lineText = null;
				int start_row=mainConfig.getStartRow() , n = 1 ;
				while ((lineText = reader.readLine()) != null) {
					if(n>=start_row){
						DZInfo  dzinfo=new DZInfo();
						if(null!=is_fiter && "1".equals(is_fiter)){
							if(lineText.contains(merchant_num)){
								dzinfo=getDzInfo(lineText, S1MAP,"S1");
								list.add(dzinfo);
							}    
						}else{
							dzinfo=getDzInfo(lineText, S1MAP,"S1");
							list.add(dzinfo);
						}
					}
				}

				if(null==list || list.size()==0){
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("获取对账数据为空");
					dzInfoArray.setDzInfo(new DZInfo[0]);
				}else{
					DZInfo[]  dzi=list.toArray(new DZInfo[list.size()]);
					dzInfoArray.setRc("1");
					dzInfoArray.setDzInfo(dzi);
				}

			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dzInfoArray;

	}

	public  DZInfo[]  readFile2(File  file,String merchant_num,String dzdate,MerchantConfig merchantConfig,String tran_type){

		MainConfig mainConfig=merchantConfig.getMainConfig();
		Map<String, Map<String,DZFieldConfig>> map=merchantConfig.getMap();
		Map<String,DZFieldConfig> MAP=new HashMap<String, DZFieldConfig>();
		if("S1".equals(tran_type)) MAP=map.get("S1");
		else if("S3".equals(tran_type)) MAP=map.get("S3");
		String is_fiter="1";
		String charset=mainConfig.getCharset();
		if(file.exists()){
			BufferedReader reader=null;
			List<DZInfo> list=new ArrayList<DZInfo>();
			try {
				reader =StringHelper.isEmpty(charset) ? new BufferedReader(new FileReader(file)): new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName(charset)));
				String lineText = null;
				int start_row=mainConfig.getStartRow();  
				int   n = 1 ;
				DZInfo  dzinfo=new DZInfo();
				while ((lineText = reader.readLine()) != null) {
					if(n>=start_row){
						if(null != is_fiter && "1".equals(is_fiter)){
							if(lineText.contains(merchant_num)){
								dzinfo = getDzInfo(lineText, MAP,tran_type);
								list.add(dzinfo);
							}
						}else{
							dzinfo = getDzInfo(lineText, MAP,tran_type);
							list.add(dzinfo);
						}		
					}
					++n;
				}

				if(null == list || list.size() == 0){
					return  new DZInfo[0];
				}else{
					DZInfo[]  dzi = (DZInfo[]) list.toArray(new DZInfo[list.size()]);
					return dzi;
				}

			}catch (Exception e){
			    LOG.addError("解析对账文件"+file.getName()+"异常");
				LOG.addError(e);
				return new DZInfo[0];
			}
		}else{
			return	 new DZInfo[0];
		}
		
	
	}

	private DZInfo getDzInfo(String line,Map<String, DZFieldConfig> map,String tran_type){
		DZInfo dzInfo=new DZInfo();
		DZFieldConfig  tremid=(DZFieldConfig)map.get("termid");
		DZFieldConfig  bankname=map.get("bankname");
		DZFieldConfig  trancardnum=map.get("trancardnum");
		DZFieldConfig  trdatetime=map.get("trdatetime");
		//DZFieldConfig  trancode =map.get("trancode");
		DZFieldConfig  amount =map.get("amount");
		DZFieldConfig  refernum=map.get("refernum");
		//DZFieldConfig  payflowno=map.get("payflowno");
		//DZFieldConfig  hisoperator=map.get("hisoperator");
		//DZFieldConfig  patientname=map.get("patientname");
		//DZFieldConfig  patientid=map.get("patientid");
		DZFieldConfig  ext1=map.get("ext1");
		DZFieldConfig  ext2=map.get("ext2");
		DZFieldConfig  ext3=map.get("ext3");
		//DZFieldConfig  ext4=map.get("ext4");
		//DZFieldConfig  ext5=map.get("ext5");
		//DZFieldConfig  ext6=map.get("ext6");
		if(tremid.isJoin()) dzInfo.setTermId(line.substring(tremid.getStart_index(), tremid.getEnd_index()));
		if(bankname.isJoin()) dzInfo.setBankName(line.substring(bankname.getStart_index(), bankname.getEnd_index()));
		if(trancardnum.isJoin()) dzInfo.setTranCardNum(line.substring(trancardnum.getStart_index(), trancardnum.getEnd_index()).trim());
		String tr_date_time = null;
		if(trdatetime.isJoin()) {
			tr_date_time = line.substring(trdatetime.getStart_index(), trdatetime.getEnd_index());
			String time= accountDate+tr_date_time.substring(4,tr_date_time.length());	
			time=DateUtil.stringFormat(time,"yyyyMMddHHmmss","yyyy-MM-dd HH:mm:ss");
			dzInfo.setTrDateTime(time);
		}
		if(amount.isJoin())  dzInfo.setAmount(Float.valueOf(line.substring(amount.getStart_index(), amount.getEnd_index())));
		if(refernum.isJoin()) {
			String refer_num = line.substring(refernum.getStart_index(), refernum.getEnd_index());
			if("S3".equalsIgnoreCase(tran_type)){
				refer_num = tr_date_time + refer_num;
			}
			dzInfo.setReferNum(refer_num);
		} 


		if(ext1.isJoin() && ext2.isJoin()  && ext3.isJoin()){
			String  TranCode=null;
			String  msgtype  =line.substring(ext1.getStart_index(), ext1.getEnd_index());
			String  proccode =line.substring(ext2.getStart_index(), ext2.getEnd_index());
			String  svr_code   =line.substring(ext3.getStart_index(),ext3.getEnd_index());
			TranCode=msgtype.substring(0,3)+proccode.substring(0,2)+svr_code;
			if("S3".equalsIgnoreCase(tran_type)){
				TranCode = "S3";
			}else{
				if("0200000".equals(TranCode))  TranCode="S1";
				else if("0202000".equals(TranCode) || "0420000".equals(TranCode))  TranCode="S2";
				else if("0222000".equals(TranCode))  TranCode="S3";
				else TranCode="";		
			}
			dzInfo.setTranCode(TranCode);
		}

		return dzInfo;

	}
}
